package com.chenwut.mydynamicproxy.a1_;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author ChenPi
 * @since 2024/09/24 11:42
 * <p>创建代理对象</p>
 */

public class ProxyUtil {

    public static StarService createProxy(BigStar bigStar) {

        StarService proxy = (StarService) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(),
                new Class[]{StarService.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if ("sing".equals(method.getName())) {
                            System.out.println("准备唱歌");
                        } else if ("dance".equals(method.getName())) {
                            System.out.println("准备跳舞");
                        }

                        return method.invoke(bigStar, args);
                    }
                });

        return proxy;
    }

}
